package cn.edu.bupt.freewings.forum.server.Utility;

import java.sql.Timestamp;
import java.util.List;

import cn.edu.bupt.freewings.forum.common.ProjectType;
import cn.edu.bupt.freewings.forum.common.UserType;
import cn.edu.bupt.freewings.forum.persistence.Project;
import cn.edu.bupt.freewings.forum.common.TopicType;
import cn.edu.bupt.freewings.forum.persistence.Topic;
import cn.edu.bupt.freewings.forum.persistence.User;
import cn.edu.bupt.freewings.forum.common.UserType;
import cn.edu.bupt.freewings.forum.persistence.Post;
import cn.edu.bupt.freewings.forum.server.rest.UserResource;
import cn.edu.bupt.freewings.forum.common.PostType;
import cn.edu.bupt.freewings.forum.server.rest.UserResource;

/**
 * @author hubaiyu (hubaiyu@gmail.com)
 * 
 */
public class Converter {

	public static UserType.User userToUserType(User user, boolean withPassword) {
		UserType.User.Builder builder = UserType.User.newBuilder();
		builder.setId(user.getId()).setName(user.getName()).setNickName(
				user.getNickName());
		if (withPassword) {
			builder.setPassword(user.getPassword());
		}
		return builder.build();
	}

	public static ProjectType.ProjectList projectListToProjectTypeList(
			List<Project> projectList, Integer projectParentId) {
		ProjectType.ProjectList.Builder projectListBuilder = ProjectType.ProjectList
				.newBuilder();
		for (Project project : projectList)
			projectListBuilder.addProject(projectToProjectType(project));
		if (projectParentId != null)
			projectListBuilder.setParent(projectParentId);
		return projectListBuilder.build();
	}

	public static ProjectType.Project projectToProjectType(Project project) {
		ProjectType.Project.Builder projectBuilder = ProjectType.Project
				.newBuilder();
		projectBuilder.setId(project.getId()).setName(project.getName()).setParent(
				project.getParent()).setCreatorId(project.getCreatorId()).setCreator(
				UserResource.getUserInfoById(project.getCreatorId()));
		return projectBuilder.build();
	}

	public static TopicType.TopicList topicListTpTopicTypeList(
			List<Topic> topicList, Integer projectId) {
		TopicType.TopicList.Builder topicListBuilder = TopicType.TopicList
				.newBuilder();
		for (Topic topic : topicList)
			topicListBuilder.addTopic(topicToTopicType(topic));
		if (projectId != null)
			topicListBuilder.setProjectId(projectId);
		return topicListBuilder.build();
	}

	public static TopicType.Topic topicToTopicType(Topic topic) {
		TopicType.Topic.Builder topicBuilder = TopicType.Topic.newBuilder();
		topicBuilder.setId(topic.getId()).setName(topic.getName()).setProjectId(
				topic.getProjectId()).setCreatorId(topic.getCreatorId()).setCreator(
				UserResource.getUserInfoById(topic.getCreatorId()));
		return topicBuilder.build();
	}

	public static PostType.PostList postListToPostTypeList(List<Post> postList,
			Integer topicId) {
		PostType.PostList.Builder postListBuilder = PostType.PostList.newBuilder();
		for (Post post : postList)
			postListBuilder.addPost(postToPostType(post));
		if (topicId != null)
			postListBuilder.setTopicId(topicId);
		return postListBuilder.build();
	}

	public static PostType.Post postToPostType(Post post) {
		PostType.Post.Builder postBuilder = PostType.Post.newBuilder();
		postBuilder.setId(post.getId()).setPostTime(post.getPostTime().getTime())
				.setText(post.getText()).setTopicId(post.getTopicId()).setUserId(
						post.getUserId()).setUser(
						UserResource.getUserInfoById(post.getUserId()));
		if (post.getReplyId() != null)
			postBuilder.setReplyId(post.getReplyId());
		return postBuilder.build();
	}
	
	public static Post postTypeToPost(PostType.Post postType){
		Post post = new Post();
		if(postType.hasPostTime())
			post.setPostTime(new Timestamp(postType.getPostTime()));
		if(postType.hasReplyId())
			post.setReplyId(postType.getReplyId());
		if(postType.hasTag())
			post.setTag(postType.getTag());
		if(postType.hasId())
			post.setId(postType.getId());
		post.setText(postType.getText());
		post.setTopicId(postType.getTopicId());
		post.setUserId(postType.getUserId());
		return post;
	}
	
	public static User UserTypeToUser(UserType.User userType){
		User user = new User();
		if(userType.hasName())
			user.setName(userType.getName());
		if(userType.hasNickName())
			user.setNickName(userType.getNickName());
		if(userType.hasPassword())
			user.setPassword(userType.getPassword());
		if(userType.hasTag())
			user.setTag(userType.getTag());
		if(userType.hasId())
			user.setId(userType.getId());
		return user;
	}
	
	public static Topic TopicTypeToTopic(TopicType.Topic topicType){
		Topic topic = new Topic();
		if(topicType.hasCreatorId())
			topic.setCreatorId(topicType.getCreatorId());
		if(topicType.hasId())
			topic.setId(topicType.getId());
		if(topicType.hasName())
			topic.setName(topicType.getName());
		if(topicType.hasProjectId())
			topic.setProjectId(topicType.getProjectId());
		if(topicType.hasTag())
			topic.setTag(topicType.getTag());
		return topic;
			
	}
}
